package com.egoo.ticket.server.controller;

import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.pojo.dto.ExternalVisitDto;
import com.egoo.ticket.server.pojo.dto.WorkOrderFieldReflectDto;
import com.egoo.ticket.server.service.WorkOrderFieldReflectService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.DbResponse;
import com.egoo.ticket.server.utils.common.ResponseConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;


/**
 * @ClassName: WorkOrderFieldReflectController
 * @Description:
 * @Author: Donny_Dong
 * @CreateDate: 2019/12/25
 * @Version: 1.0
 **/
@Slf4j
@RestController
@RequestMapping(value = "/v1/workOrderFieldReflect",produces = "application/json")
@Api(value = "工单字段映射管理",tags = "工单字段映射管理",description = "工单字段映射管理")
public class WorkOrderFieldReflectController {
	@Autowired
	private WorkOrderFieldReflectService workOrderFieldReflectService;
	@Value("${server.external-create}")
	private String externalCreate;


	@ApiOperation(value = "字段映射类型创建",notes = "字段映射类型创建")
	@RequestMapping(method = RequestMethod.POST,value = "/create")
	public DbResponse createWorkOrderFieldReflect(@RequestBody WorkOrderFieldReflectDto workOrderFieldReflectDto,
												  HttpServletRequest request){
		String userId = request.getHeader(ConstantCode.CurrentUserInfo.USER_ID);
		String tenantId = request.getHeader(ConstantCode.CurrentUserInfo.TENANT_ID);
		if (CollectionUtils.isEmpty(workOrderFieldReflectDto.getWorkOrderFieldReflect())){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"创建映射字段信息为空");
		}
		if (StringUtils.isEmpty(workOrderFieldReflectDto.getWorkOrderTypeId())){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"工单类型为空");
		}

		return workOrderFieldReflectService.creat(workOrderFieldReflectDto);

	}


	@ApiOperation(value = "获取工单映射字段对应含义",notes = "获取工单字段对应含义")
	@RequestMapping(method = RequestMethod.GET,value = "/getWorkOrderFieldMeaning")
	public DbResponse getWorkOrderFieldMeaning(@ApiParam(value = "外部模板Id", required = true) @RequestParam(name = "id") Integer id){
		if (id == null || "".equals(id)){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"请选择一个模板");
		}
		DbResponse dbResponse = workOrderFieldReflectService.getWorkOrderFieldMeaning(id);

		return dbResponse;

	}

	@ApiOperation(value = "外部创建请求参数展示",notes = "外部创建请求参数展示")
	@RequestMapping(method = RequestMethod.GET,value = "/externalRequestParamShow")
	public DbResponse externalRequestParamShow(@ApiParam(value = "工单类型Id", required = true) @RequestParam(name = "id") Long id
											  ){
		if (id == null || "".equals(id)){
			return new DbResponse(ResponseConstant.RES_FAIL_CODE,"id不能为空");
		}

		JSONObject jsonObject = new JSONObject();
		ArrayList<ExternalVisitDto> arrayList = new ArrayList<>();


		arrayList.add(new ExternalVisitDto("workOrderTypeId","流程编号(需提供所展示的数据)",id.toString()));
		arrayList.add(new ExternalVisitDto("createrId","创建人编号(需在调用接口时提供)",""));
		arrayList.add(new ExternalVisitDto("createrName","创建人名(需在调接口时提供)",""));
		jsonObject.put("requestParam",arrayList);
		jsonObject.put("requestUrl", externalCreate);
		jsonObject.put("requestWay","post");


		return new DbResponse(ResponseConstant.RES_SUCCESS_CODE,"数据获取成功",jsonObject);

	}

}
